<?php

namespace DC\UserCenter\Models;

use App\Helpers\SelfEncrypt;
use App\Models\BaseModel;

class UserAuthModel extends BaseModel
{
    protected $table = 'users_auth';

    protected $primaryKey = 'member_id';

    public $dateFormat = 'U';

    public $guarded = [];

    protected $dates = [
        'created_at',
        'updated_at',
        'activated_at',
        'frozen_at'
    ];

    public function base()
    {
        return $this->belongsTo('DC\UserCenter\Models\UserBaseModel');
    }

    public function handleData($data)
    {

        $login_salt = SelfEncrypt::randStr(8);
        $login_pwd = $this->encryptPassword($data['login_pwd'],$login_salt);
        $safe_salt = SelfEncrypt::randStr(8);
        $safe_pwd = $this->encryptPassword($data['safe_pwd'],$safe_salt);

        $auth['login_pwd'] = $login_pwd;
        $auth['login_salt'] = $login_salt;
        $auth['safe_pwd'] = $safe_pwd;
        $auth['safe_salt'] = $safe_salt;
        $auth['created_ip'] = ip2long(request()->getClientIp());
        $auth['status'] = 0;

        return $auth;
    }

    public function updateLoginPwd($login_pwd,$id)
    {
        $info = self::find($id);
        if(!$info){
            $this->errorMsg = '要修改密码的账号信息不存在';
            return false;
        }

        $login_salt = SelfEncrypt::randStr(8);
        $login_pwd = $this->encryptPassword($login_pwd,$login_salt);
        $info->login_pwd = $login_pwd;
        $info->login_salt = $login_salt;
        if(!$info->save()){
            return false;
        }
        return true;
    }

    public function updateSafePwd($safe_pwd, $id)
    {
        $info = self::find($id);
        if(!$info){
            $this->errorMsg = '要修改密码的账号信息不存在';
            return false;
        }

        $safe_salt = SelfEncrypt::randStr(8);
        $safe_pwd = $this->encryptPassword($safe_pwd,$safe_salt);
        $info->safe_pwd = $safe_pwd;
        $info->safe_salt = $safe_salt;
        if(!$info->save()){
            return false;
        }
        return true;
    }

    /**
     * @title 检测登录密码
     * @param $login_pwd
     * @param $id
     * @return bool
     */
    public function checkLoginPwd($login_pwd,$id)
    {
        $info = self::find($id,['member_id','login_pwd','login_salt']);
        if(!$info){
            return false;
        }
        if(!$this->checkPassword($login_pwd,$info->login_salt,$info->login_pwd)){
            return false;
        }
        return true;
    }

    public function checkSafePwd($safe_pwd, $id)
    {
        $info = self::find($id,['member_id','safe_pwd','safe_salt']);
        if(!$info){
            return false;
        }
        if(!$this->checkPassword($safe_pwd,$info->safe_salt,$info->safe_pwd)){
            return false;
        }
        return true;
    }

    private function encryptPassword($pwd, $salt)
    {
        return SelfEncrypt::make($pwd.$salt);
    }

    private function checkPassword($str, $salt, $pwd)
    {
        return SelfEncrypt::check($str.$salt,$pwd);
    }
}
